//
//  applyVipViewController.m
//  AllFamily
//
//  Created by chirui on 2018/6/1.
//  Copyright © 2018年 Shui. All rights reserved.
//

#import "applyVipViewController.h"

@interface applyVipViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    UITableView * _tableView;
    
    NSInteger indes;
    NSString * typeStr;
}

@end

@implementation applyVipViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    indes = 0;
    typeStr = @"service";  //服务站
    
    TitleView(@"申请等级");
    [self creatTableView];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_nav_renter" target:self action:@selector(backBtnClick)];
}

-(void)backBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0,0, WIDTH, HEIGHT-SH_NavBarHeight) style:UITableViewStyleGrouped];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView *view = [[UIView alloc]init];
    _tableView.tableFooterView = view;
    
    [self.view addSubview:_tableView];
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 7;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 70;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 120;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString * cellId =@"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }else{
        while ([cell.contentView.subviews lastObject]) {
            
            [[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    
    cell.contentView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    UIView * view  = [[UIView alloc]initWithFrame:CGRectMake(15, 10, WIDTH - 30, 50)];
    
    view.backgroundColor = [UIColor whiteColor];
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, WIDTH-30, 50);
    btn.tag = indexPath.row;
    
    
    if (indexPath.row==0) {
        [btn setTitle:@"申请成为服务站" forState:UIControlStateNormal];
    }else  if (indexPath.row==1){
        
        [btn setTitle:@"申请成为区县代理" forState:UIControlStateNormal];
    }else  if (indexPath.row==2){
        
        [btn setTitle:@"申请成为市代理" forState:UIControlStateNormal];
    }else  if (indexPath.row==3){
        
        [btn setTitle:@"申请成为省代理" forState:UIControlStateNormal];
    }else  if (indexPath.row==4){
        
        [btn setTitle:@"申请成为大区经理" forState:UIControlStateNormal];
    }else  if (indexPath.row==5){
        
        [btn setTitle:@"申请成为大区总监+2%分红" forState:UIControlStateNormal];
    }else{
        
         [btn setTitle:@"申请成为参与分红" forState:UIControlStateNormal];
    }
    
    if (indes == indexPath.row) {
        
        [btn setBackgroundImage:ImageName(@"img_user_grade_bg") forState:UIControlStateNormal];
    }else{
        
         [btn setBackgroundImage:ImageName(@"") forState:UIControlStateNormal];
    }
    
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    
    [cell.contentView addSubview:view];
    
    
    return cell;
}

-(void)btnClick:(UIButton *)btn{
    
    indes = btn.tag;
    if (indes ==0) {
        
        typeStr = @"service";
    }else if(indes==1){
        
        typeStr = @"run";
    }else if(indes==2){
        
        typeStr = @"city";
    }else if(indes==3){
        
        typeStr = @"province";
    }else if(indes==4){
        
        typeStr = @"area";
    }else if(indes==5){
        
        typeStr = @"manager";
    }else {
        
         typeStr = @"officer";
    }
    
    [_tableView reloadData];
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    indes = indexPath.row;
    
    if (indes ==0) {
        
        typeStr = @"service";
    }else if(indes==1){
        
        typeStr = @"run";
    }else if(indes==2){
        
        typeStr = @"city";
    }else if(indes==3){
        
        typeStr = @"province";
    }else if(indes==4){
        
        typeStr = @"area";
    }else if(indes==5){
        
        typeStr = @"manager";
    }else {
        
        typeStr = @"officer";
    }
    
    [_tableView reloadData];
    
    
}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    
    UIView * view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 120)];
    view.backgroundColor =[UIColor clearColor];
    
    UIButton * btn =[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(40, 10, WIDTH- 80, 80);
    btn.layer.cornerRadius = 20.f;
    btn.layer.masksToBounds = YES;
    [btn setBackgroundImage:ImageName(@"icon_login__buttton_bg") forState:UIControlStateNormal];
    [btn setTitle:@"确定" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(takeOut:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    return view;
    
}

-(void)takeOut:(UIButton *)btn{
    
    NSDictionary * dic = @{@"mid":USER_DEFAULTGet(@"mid"),
                           @"token":USER_DEFAULTGet(@"token"),
                           @"level":typeStr
                           };
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(apply_lever_api) andparameters:dic Success:^(id responseObject) {
        if (codeSucess) {
            SHuiMBhudTextTimeStr(@"申请成功！");
            
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW , (1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{
                    
                    [self.navigationController popViewControllerAnimated:YES];
                });
            });
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        SHuiMBhudTextNetError;
    }];
    
    
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return nil;
}

@end
